package jzqb.annotation.CommentCheck;

import haha.life.normal.TokenUtils;
import jzqb.common.Result;
import jzqb.dao.AdminUserInfoDao;
import jzqb.po.AdminUserPo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class CommentCheckAspect {

    @Autowired AdminUserInfoDao adminUserInfoDao;

    //定义切点
    @Pointcut("@annotation(jzqb.annotation.CommentCheck.CommentCheck)")//注意这个写类的全路径
    public void commentCheckPoint(){

    }

    @Before("commentCheckPoint()")
    public void beforePoint(JoinPoint joinPoint){

    }

    //切点后执行
    @After("commentCheckPoint()")
    public void afterPoint(){

    }

    @Around("commentCheckPoint()")
    public Object  around(ProceedingJoinPoint pjp) throws Throwable {

        //获取token
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = req.getHeader("token");
        System.out.println(token);
        //解析token获取id
        String userID =  TokenUtils.checkTokenReturnID(token);

        System.out.println("登录人是:" + userID);
        if (userID == null){
            return Result.fail("token错误或过期,请重新登录");
        }

        AdminUserPo user = adminUserInfoDao.findUserById(userID);
        String mute = user.getMuteFlag();
        if ("1".equals(mute)){
            return Result.fail("抱歉!您已被禁言");
        }else{
            return pjp.proceed();
        }

    }

    //切面
    @AfterReturning("commentCheckPoint()")
    public void saveLog(JoinPoint joinPoint){
    }

}
